// services/modalService.ts 弹窗服务类
import { getModalState } from './modalState'
import { ModalConfig } from './modal.ts'

class ModalService {
  private getState() {
    return getModalState()
  }

  open(config: ModalConfig) {
    const state = this.getState()

    // 合并默认配置
    const defaultConfig: Partial<ModalConfig> = {
      closable: true,
      maskClosable: true,
      showHeader: true,
      showFooter: true,
      width: 520,
    }

    state.config.value = { ...defaultConfig, ...config }
    state.visible.value = true

    // 触发打开回调
    config.onOpen?.()
  }

  close() {
    const state = this.getState()
    state.visible.value = false

    // 触发关闭回调
    state.config.value.onClose?.()
  }

  // 更新按钮加载状态
  updateButtonLoading(buttonIndex: number, loading: boolean) {
    const state = this.getState()
    if (state.config.value.buttons?.[buttonIndex]) {
      state.config.value.buttons[buttonIndex].loading = loading

      // 触发响应式更新
      state.config.value = { ...state.config.value }
    }
  }

  // 更新配置
  updateConfig(newConfig: Partial<ModalConfig>) {
    const state = this.getState()
    state.config.value = { ...state.config.value, ...newConfig }
  }

  // 快捷方法 - 确认对话框
  confirm(config: Omit<ModalConfig, 'buttons' | 'showFooter'>) {
    return new Promise<boolean>((resolve) => {
      this.open({
        ...config,
        showFooter: true,
        buttons: [
          {
            text: '取消',
            onClick: () => resolve(false),
            autoClose: true,
          },
          {
            text: '确定',
            type: 'primary',
            onClick: () => resolve(true),
            autoClose: true,
          },
        ],
      })
    })
  }

  // 快捷方法 - 警告框
  alert(config: Omit<ModalConfig, 'buttons' | 'showFooter'>) {
    return new Promise<void>((resolve) => {
      this.open({
        ...config,
        showFooter: true,
        buttons: [
          {
            text: '确定',
            type: 'primary',
            onClick: () => resolve(),
            autoClose: true,
          },
        ],
      })
    })
  }

  // 快捷方法 - 成功提示
  success(config: Omit<ModalConfig, 'buttons' | 'showFooter'>) {
    return this.alert({
      ...config,
      title: config.title || '操作成功',
      content: config.content || '操作已完成',
    })
  }

  // 快捷方法 - 错误提示
  error(config: Omit<ModalConfig, 'buttons' | 'showFooter'>) {
    return this.alert({
      ...config,
      title: config.title || '操作失败',
      content: config.content || '操作过程中出现错误',
    })
  }

  // 快捷方法 - 加载中弹窗
  loading(config: Omit<ModalConfig, 'buttons' | 'closable' | 'maskClosable'>) {
    this.open({
      ...config,
      closable: false,
      maskClosable: false,
      showFooter: false,
      title: config.title || '加载中...',
    })
  }
}

// 创建单例
let modalServiceInstance: ModalService | null = null

export const getModalService = (): ModalService => {
  if (!modalServiceInstance) {
    modalServiceInstance = new ModalService()
  }
  return modalServiceInstance
}

// 默认导出单例
export const modalService = getModalService()
